package com.itqf.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:  处理所有controller的异常
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/16
 * @Time: 下午5:38
 */
@ControllerAdvice //通用
//@RestControllerAdvice   @RestController
public class MyExceptionHandler {

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody   //ajax  响应json
    public Map<String,Object> exception(ArithmeticException e){

        Map m = new HashMap();
        m.put("code","500");
        m.put("errorMsg","算术异常");

        return  m;
    }

    //跳页面
//    @ExceptionHandler(ArithmeticException.class)
//    public ModelAndView exception(ArithmeticException e){
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("error.jsp");
//        modelAndView.addObject("errorMsg","算术异常");
//        return  modelAndView;
//    }

    @ExceptionHandler(Exception.class)
    @ResponseBody   //响应json
    public Map<String,Object> exception(Exception e){

        Map m = new HashMap();
        m.put("code","500");
        m.put("errorMsg",e.getMessage());

        return  m;
    }
}
